<?php

namespace app\admin\model;

use think\facade\Session;
use think\Model;

class Auth extends Model
{
   // 返回用户所有的权限网页
   public static function get($userId)
   {
      $userData = User::where('id', $userId)->find();
      // [1,2,3,4] Role 表 对应的权限表
      $url = [];
      if ($userData && !empty($userData->role)) {
         $role = $userData->role;
         $roleArr = explode(',', $role);
         foreach ($roleArr as $k => $v) {
            if ($v != 'all') {
               $role = Role::find($v);
               if ($role) {
                  $auth = explode(',', $role->auth);
                  foreach ($auth as $m => $n) {
                     $uri = self::find($n);
                     if ($uri) {
                        if (!array_key_exists($uri->url, $url)) {
                           array_push($url, $uri->url);
                        }
                     }
                  }
               }
            } else {
               $url = 'all';
            }
         }
      }
      return $url;
   }
   public static function list()
   {
      $list = self::where('pid', 0)->select();
      foreach ($list as $k => $v) {
         $list[$k]['child'] = self::where('pid', $v->id)->select();
      }
      return $list;
   }
}
